<template>
  <div class="xtx-checkbox" @click="changeChecked">
 <i
      class="iconfont "
      :class="modelValue ? 'icon-checked' : 'icon-unchecked'"
    />
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>

<script>
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxCheckbox',
  props: {
    // 子组件接收布尔值
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  // setup (props, { emit }) {
  //   const changeChecked = () => {
  //     // 点击取反后的布尔值传递给父组件
  //     const temp = !props.modelValues
  //     emit('update:modelValue', temp)
  //   }
  //   return { changeChecked }
  // }
  // vue3的usemodes
  setup (props, { emit }) {
    const data = useVModel(props, 'modelValue', emit)
    const changeChecked = () => {
      data.value = !data.value
    }
    return { data, changeChecked }
  }

}
</script>

<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
